<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.model.CourseContainer" %>
<%@ page import="schoolsource.model.Item" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%  
    SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
    String requestObject = (String) st.getValue(FormFields.TOKEN);
    CourseContainer courseContainer = (CourseContainer) st.removeValue("courseContainer"); 
    Item assignedClassList[][] = (Item[][]) st.removeValue("assignedClassList"); 
    Item assignedTeacherList[][][] = (Item[][][]) st.removeValue("assignedTeacherList");
    String editStatus = (String) st.removeValue(FormFields.EDITSTATUS);
    int editorPage = (new Integer((String) st.removeValue(FormFields.EDITORPAGE))).intValue();
    String displayMessage = (String) st.getValue("displayMessage"); 
    st.putValue("displayMessage", "");
	String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
	String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
%>



<HTML>

<HEAD>

  <title>Course Editor Form</title>

</HEAD>

    <SCRIPT LANGUAGE="JavaScript">

        function validateRequiredFields() {
            if (document.forms[0].<%= SchoolDBSQL.COURSESCHOOLID %>.value == "") {
                alert("You must enter an ID for the course.");
                document.forms[0].<%= SchoolDBSQL.COURSESCHOOLID %>.focus();
                return false;
            }
            else {
                return true;
            }
        }

        function cancelForm() {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.CANCEL %>";
            document.forms[0].submit();
        }

        function viewDetailedInfo(page, selectedItem) {
            top.frames.mainframe.document.forms[0].<%= FormFields.SUBLINK %>.value = selectedItem;
            top.frames.mainframe.submitPage(top.frames.mainframe.document, "<%= FormFields.EDITORPAGE %>", page);
            return false;
        }

        function postProcessing(x) {
            if (validateRequiredFields()) {
                document.forms[0].<%= FormFields.SAVECOURSE %>.value = x;
                document.forms[0].submit();
            }
        }

        function init() {
          if ("<%= displayMessage %>" != "null" && "<%= displayMessage %>" != "") {
            top.frames.mainframe.notify("<%= displayMessage %>");
          }
        }
  
    </SCRIPT>
    <body onLoad="init();
    	<%
		if (editStatus.equals(FormFields.VIEW) && courseContainer != null) {
        	out.print("top.frames.mainframe.highlightByID('" + courseContainer.getCourseAssignedID() + "');");
        }
        %>">

    <FORM NAME="courseeditorform" METHOD="POST" onSubmit="return false;">
        <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.COURSEASSIGNEDID %>" <%= (st.removeValue("adding") == null) ? "onFocus=\"blur()\"" : "" %>
           VALUE="<%= (courseContainer!=null) ? courseContainer.getCourseAssignedID().toString() : "nothing there"%>"> 
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="<%= FormFields.EDCOURSEPAGE %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="<%= FormFields.SAVEACTION %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.EDITORPAGE %>" VALUE="<%= editorPage %>"> 
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">

        <H2>Course Editor</H2><br>
            <TABLE WIDTH="600">
            <TR>
                <TH ALIGN="LEFT">Course ID</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.COURSESCHOOLID + "\" VALUE=\"" + courseContainer.getCourseSchoolID().toString() + "\">");
                        }
                        else {
                            out.println(courseContainer.getCourseSchoolID().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Course Name</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("<INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.COURSESHORTNAME + "\" VALUE=\"" + courseContainer.getCourseShortName().toString() + "\">");
                        }
                        else {
                            out.println(courseContainer.getCourseShortName().toString());
                        }
                    %>
                </TD>
            </TR>
            <TR>
                <TH ALIGN="LEFT">Description</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("<INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.COURSEDESCRIPTION + "\" VALUE=\"" + courseContainer.getCourseDescription().toString() + "\">");
                        }
                        else {
                            out.println(courseContainer.getCourseDescription().toString());
                        }
                    %>
                </TD>
            </TR>

                <TD COLSPAN=2>&nbsp;</TD>
            <%
                if (editStatus.equals(FormFields.VIEW)) {
                    out.println("            <TR>");
                    out.println("                <TH ALIGN=\"LEFT\" VALIGN=\"TOP\">Course Sessions</TH>");
                    out.println("                <TD>");
                    out.println("                  <TABLE>");
                    out.println("                    <TR>");
                    out.println("                      <TH>Class</TH>");
                    out.println("                      <TH>&nbsp;&nbsp;&nbsp;&nbsp;</TH>");
                    out.println("                      <TH>Teacher</TH>");
                    out.println("                    </TR>");
                    for (int i = 0; i < assignedClassList.length; i++) {
                        out.println("                    <TR>");
                        out.println("<TD><a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDCLASSPAGE + "','" + assignedClassList[i][0] + "');return false;\">" + assignedClassList[i][1] + "&nbsp;&nbsp;&nbsp;" + assignedClassList[i][2] + "</a></TD>\n<TD>&nbsp;</TD>\n<TD>");
                        for (int j = 0; j < assignedTeacherList[i].length; j++) {
                            out.println("<a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDTEACHERPAGE + "','" + assignedTeacherList[i][j][0] + "');return false;\">" + assignedTeacherList[i][j][2] + ", " + assignedTeacherList[i][j][1] + "</a>");
                        }
                        out.println("</TD>");
                        out.println("                    </TR>");
                    }
                    out.println("                  </TABLE>");
                    out.println("                </TD>");
                    out.println("            </TR>");
                }
            %>
            <TR>
                <TD COLSPAN=2>&nbsp;</TD>
            </TR>
            <TR>
                <%
                    if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                        out.println("                <TD><INPUT NAME=\"" + FormFields.SAVECOURSE  + "\" TYPE=\"BUTTON\" VALUE=\"Save\" onClick=\"postProcessing('Save')\"></TD>");
                        out.println("                <TD><INPUT NAME=\"cancel\" TYPE=\"BUTTON\" VALUE=\"Cancel\" onClick='cancelForm()'></TD>");
                    }
                %>
            </TR>

        </TABLE>

    </FORM>
</HTML>

